package comm.suger.web.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ibm.icu.text.DateFormat;

import comm.suger.web.model.entity.AppG;
import comm.suger.web.model.entity.AppLog;
import comm.suger.web.service.AppGService;
import comm.suger.web.service.AppLogService;
import comm.suger.web.utils.DateUtils;
import comm.suger.web.utils.HightChartsBean;
/**
 *  报表处理控制器
 * @author wxb
 *
 */
@Controller
@RequestMapping("/charts")
public class ChartController {

	@Resource(name="appLogService")
	AppLogService appLogService;
	
	@Resource(name="appGService")
	AppGService appGService;
	//['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun','Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] x轴数据源
	//返回日期
	/*
	 * {"dataXList":
	 * [1413820800000,1413734400000,1413648000000,1413561600000,1413475200000,1413388800000,1413302400000],
	 * "dataYList":[{data":[96,84,75,61,20,1257,175]}]}
	 * [{
	    name: '东京',
	    data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
			}, {
			    name: '纽约',
			    data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
			}]
		 Y轴数据源
*/
	@RequestMapping(value="/chart")
	public @ResponseBody Map getData(@RequestParam("date") String strdates){
		
		Map<String, List<?>> mat = new LinkedHashMap<String, List<?>>();
		Date date = null;
		try {
			if(strdates.equals("")){
				//根据时间获取当前日期前几天的数据返回给前台
				date = DateUtils.stringToDate(DateUtils.parseDate(new Date()), "yyyy-MM-dd");
			}else{
				//前台传过来的时间
				date=DateUtils.stringToDate(strdates, "yyyy-MM-dd");
			}
			
			List<AppLog> appLogList =appLogService.getAppLog(date);
			List<AppG> appGList =appGService.getAppG(date);
			List<String> dateList = new ArrayList<String>();
			
			List<Integer> dateyDLists = new ArrayList<Integer>();//y轴数据点击量
			List<Integer> dateGLists  = new ArrayList<Integer>(); //y轴数据广告量
			
			/**倒序取出数据*/
			int length = appLogList.size();
			for(int i= length-1 ;i >= 0; i--){
				AppLog appLog =appLogList.get(i);
				AppG ag = appGList.get(i);
				
				String dates = DateUtils.parseDate(appLog.getAdate(),"yyyy-MM-dd");//后台时间转换一下
				dateList.add(dates); //X轴的时间数据
				dateyDLists.add(appLog.getAcount());//插入点击数据
				dateGLists.add(ag.getGcount());//插入广告数据
			}
			
			mat.put("dataXList",dateList);//X轴时间数据
			mat.put("dataYDList",dateyDLists);//Y轴点击量的数据
			mat.put("dataYGList",dateGLists);//Y轴点击量的数据
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return mat;
	}
	
}
